<template>
  <el-dialog
    title="添加员工"
    :visible="showDialog"
    width="40%"
    @close="closeFn"
  >
    <el-form ref="form" :rules="rules" :model="formList" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formList.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formList.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formList.password" placeholder="请输入密码" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="editFn">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getyuangonguser } from '@/api/yuangong'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formList: {
        username: '',
        email: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在3到5个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在6到16个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 取消按钮
    editFn() {
      this.closeFn()
    },
    // 确认添加
    async submit() {
      // 表单校验
      this.$refs.form.validate()
      // 添加员工
      await getyuangonguser(this.formList)
      // 关闭弹窗
      this.closeFn()
      // 更新页面的经纪管理
      this.$emit('updateEvent')
      // 更新页面页码数量
      this.$emit('updateTotal')
    },
    // 封装退出功能
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空校验规则
      this.$refs.form.resetFields()
      //   清空数据
      this.formList = {
        username: '',
        email: '',
        password: ''
      }
    }
  }
}
</script>

<style>

</style>
